﻿using Kevin.Shop.Business.Core.Components;
using Kevin.Shop.Business.Products.Components;
using Kevin.Shop.Business.Service.Entity.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RiderShop.Mobile.Common.Component;
using Kevin.Shop.Business.Comments.Components;
using Kevin.Shop.Business.Service.Entity.Products.Enumerations;
using RiderShop.Mobile.ServiceEntity.Product;
using Kevin.Shop.Business.Core.Components.Responser;
using Kevin.Shop.Business.Products.Components.Content;

namespace RiderShop.Mobile.Modle
{
    public class ProductModel
    {
        public ProductBasicInfoExtend Product { get; set; }
        public string OrderNumber { get; set; }
        public ServiceResponser Result { get; set; }

        #region Get Product Detail
        /// <summary>
        /// 根据产品编号得到产品详细信息
        /// </summary>
        /// <param name="prd">产品编号</param>
        /// <returns></returns>
        public void GetProductByProductId(string prd)
        {
            List<ProductBasicInfoExtend> product = RestfulHelper.Query<ProductBasicInfoExtend>(SiteUrl.FormatUrl("ProductManagement_GetProductInfoDetailed", prd));
            //商品存在
            if (product.FirstOrDefault() != null)
            {
                List<ProductImage> imageList = new List<ProductImage>();
                //图片集合
                //foreach (var item in product[0].FullImageList)
                //{
                //    ProductImage image = new ProductImage();
                //    //FullImageList属性不具有写权限，所以只能重新构造个ImageList
                //    image.ImageName = FormatProduct.FormatProductImageUrl(ImageType.Size350, product[0].ProductId, item.ImageName);
                //    imageList.Add(image);
                //}
                //因为轮播是反的,所以倒叙排列
                for (int i = product[0].FullImageList.Count; i > 0; i--)
                {
                    ProductImage image = new ProductImage();
                    //FullImageList属性不具有写权限，所以只能重新构造个ImageList
                    image.ImageName = FormatProduct.FormatProductImageUrl(ImageType.Size640, product[0].ProductId, product[0].FullImageList[i-1].ImageName);
                    imageList.Add(image);
                }
                product[0].ImageList = imageList;
                Product = product.FirstOrDefault();
            }
        }
        #endregion

        #region Get Product Description
        /// <summary>
        /// 根据产品编号得到产品描述
        /// </summary>
        /// <param name="prd">产品编号</param>
        /// <returns></returns>
        public List<ProductBasicInfoExtend> GetProductDescriptionByProductId(string prd)
        {
            List<ProductBasicInfoExtend> product = RestfulHelper.Query<ProductBasicInfoExtend>(SiteUrl.FormatUrl("ProductManagement_GetProductDescriptionInfo", prd));
            
            return product;
        }
        #endregion    
   
        /// <summary>
        /// 
        /// </summary>
        /// <param name="productID"></param>
        /// <param name="selectProperty"></param>
        /// <returns></returns>
        public string GetSelectPropertyProduct(string productID, string selectProperty)
        {
            string selectProductID = string.Empty;
            if (!string.IsNullOrEmpty(selectProperty))
            {
                //获取用户选择的属性集合
                //遍历筛选所有group product property 集合中同时存在这几种属性value值的product
                List<string> propertyNameList = selectProperty.Trim(',').Split(',').ToList();
                List<ProductPropertyInfo> PropertyList = new List<ProductPropertyInfo>();
                PropertyList = RestfulHelper.Query<ProductPropertyInfo>(SiteUrl.FormatUrl("ProductManagement_GetAllProductGroupPropertyByProductID", productID));
                var tempPropertyList = new List<ProductPropertyInfo>();
                if (PropertyList != null && PropertyList.Count > 0)
                {
                    PropertyList.ForEach(p =>
                    {
                        if (propertyNameList.Exists(q =>
                            q.Trim() == p.ProductUserInputOrValue.Trim()))
                        {
                            tempPropertyList.Add(p);
                        }
                    });

                    var result = tempPropertyList.GroupBy(s => new { s.ProductId, }).Select(g => new { g.Key.ProductId, count = g.Count() }).OrderByDescending(p => p.count);

                    var product = result.FirstOrDefault(p => p.count == propertyNameList.Count);
                    if (product != null)
                    {
                        selectProductID = product.ProductId;
                    }

                }
            }
            return selectProductID;
        }


        /// <summary>
        /// 根据商品编号得到商品的分组属性
        /// </summary>
        /// <param name="productId">商品编号</param>
        /// <returns>分组集合,每个分组里面包含了所对应的属性集合</returns>
        public List<ProductPropertyInfo> GetProductPropertyGroup(string productId)
        {
            List<ProductPropertyInfo> propertyList = new List<ProductPropertyInfo>();
            propertyList = RestfulHelper.Query<ProductPropertyInfo>(SiteUrl.FormatUrl("ProductManagement_GetAllProductGroupPropertyByProductID", productId));

            var displayPropertyList = new List<ProductPropertyInfo>();
            if (propertyList != null && propertyList.Count > 0)
            {
                //得到需要高亮显示属性的集合（即当前item的属性）
                var selectPropertyList =
                    (from ProductPropertyInfo property in propertyList
                     where property.ProductId.Trim() == productId.Trim()
                     select property.ProductUserInputOrValue).ToList();

                //每一种属性，需要过滤掉重复的属性值，比如 集合中是 1cm 2cm 2cm 3cm 
                //那么UI上显示就是 1cm 2cm 3cm
                //同时设置高亮属性显示
                propertyList.ForEach(p =>
                {
                    if (!displayPropertyList.Exists(q =>
                        q.ProductUserInputOrValue == p.ProductUserInputOrValue))
                    {
                        if (selectPropertyList.Exists(q =>
                            q.Trim() == p.ProductUserInputOrValue.Trim()))
                        {
                            p.SelectCss = "selected";
                            p.IsChecked = true;
                        }
                        else
                        {
                            p.SelectCss = "unselected";
                            p.IsChecked = false;
                        }
                        displayPropertyList.Add(p);
                    }
                });
            }
            return displayPropertyList;
        }

        public ServiceResponser AddFavority(string productId, decimal unitPrice)
        {
            ServiceResponser serviceResponser;
            if (WebClientContext.Current.UserId <= 0)
            {
                return new ServiceResponser();
            }
            string url = SiteUrl.FormatUrl("ProductManagement_AddProductToFavorite", WebClientContext.Current.UserId, productId, unitPrice);
            serviceResponser = RestfulHelper.UpdateWithQueryString(url);
            return serviceResponser;
        }
    }
}
